
package com.xifan.news.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xifan.news.R;
import com.xifan.news.data.NewsInfo;

import java.util.List;

public class NewsListAdapter extends BaseAdapter {
    private Context mContext;
    private List<NewsInfo> newsList;

    public NewsListAdapter(Context context, List<NewsInfo> list) {
        mContext = context;
        newsList = list;
    }

    @Override
    public int getCount() {
        return newsList.size();
    }

    @Override
    public Object getItem(int position) {
        return newsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {
        ViewHolder holder;
        if (v == null) {
            holder = new ViewHolder();
            v = View.inflate(mContext, R.layout.view_news_list, null);
            holder.newsTitle = (TextView) v.findViewById(R.id.news_title);
            holder.newsDate = (TextView) v.findViewById(R.id.news_date);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        holder.newsTitle.setText(newsList.get(position).getTitle());
        holder.newsDate.setText(newsList.get(position).getDate());
        return v;
    }

    class ViewHolder {
        TextView newsTitle;
        TextView newsDate;
    }

}
